//
//  SubjectsViewController.m
//  xiaoyiNew
//
//  Created by TW on 2017/2/8.
//  Copyright © 2017年 讯一. All rights reserved.
//

#import "SubjectsViewController.h"
#import "UIButton+CustomButton.h"

static  NSInteger const kClassifyBtnTag = 150;
@interface SubjectsViewController (){

    NSArray * _titles;
    NSArray * _images;
}

@property(nonatomic, strong) UIButton * blackBut;//返回
@property(nonatomic, strong) UIView * classifyView;//分类


@end

@implementation SubjectsViewController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initializeDataSource];
    [self initializeUserInterface];
    [self initializeConstraints];
    // Do any additional setup after loading the view.
}



- (void)initializeDataSource {
    //更新数据
    _titles = @[@"逛吃",@"达人",@"美妆",@"宅男",@"腐女",@"招领"];
    _images = @[@"merenq4",@"merenq4",@"merenq4",@"merenq4",@"merenq4",@"merenq4"];
}
- (void)initializeUserInterface {
    //初始化界面
    [self.view addSubview:self.blackBut];
    [self.view addSubview:self.classifyView];
}
- (void)initializeConstraints {
    //设置约束
    [self.blackBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.left.equalTo(self.view).offset(15);
        make.size.mas_equalTo(CGSizeMake(30, 25));
    }];
    [self.classifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(Screen_Width, 200));
    }];
}

#pragma mark - 点击 -
//返回
- (void)action_blackClick {

    [self.navigationController popViewControllerAnimated:YES];
}
//分类
- (void)respondClassifyButton:(UIButton *)sender {

    NSInteger caseNum = sender.tag - kClassifyBtnTag;
    if (_delegate && [_delegate respondsToSelector:@selector(subjectsChangeWithStr:)]) {
        [_delegate subjectsChangeWithStr:_titles[caseNum]];
    }
    [self action_blackClick];
}
#pragma mark - lazy -
- (UIButton *)blackBut {

    
    if (!_blackBut) {
        _blackBut = [UIButton buttonWithType:0];
        [_blackBut setImage:[UIImage imageNamed:@"black"] forState:0];
        [_blackBut addTarget:self action:@selector(action_blackClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _blackBut;
    
}
- (UIView *)classifyView{
    if(!_classifyView){
        _classifyView = [[UIView alloc]init];
        _classifyView.backgroundColor = [UIColor whiteColor];
        CGFloat btnW = Screen_Width / 3;
        CGFloat btnH = 220 / 2;
        [_titles enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:obj forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:_images[idx]] forState:UIControlStateNormal];
            btn.tag = kClassifyBtnTag + idx;
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            [btn addTarget:self action:@selector(respondClassifyButton:) forControlEvents:UIControlEventTouchUpInside];
            btn.frame = CGRectMake(btnW * (idx % 3), btnH * (idx / 3), btnW, btnH);
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn verticalImageAndTitle:5];
            [_classifyView addSubview:btn];
        }];
    }
    return _classifyView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
